import urllib
import requests
import json
import datetime, time


deviceList = []

def getDeviceList(id):
    global deviceList
    r = requests.get('https://flocwise.cn/getDeviceList?user_id='+id, verify=False)
    if r.status_code == 200:
        deviceList = json.loads(r.text)['List']        
        selectList = []
        for device in deviceList:
            # print(device)
            selectList.append(device['SN'] + ' '+device['Name'])
        selectList = tuple(selectList)
        return selectList
    else:
        print('获取设备信息错误')
        return ()

def getRecentImageLink(SN,num, t, key):        
    print('key',key)
    print('https://flocwise.cn/imgRecentLink?num=%s&time=%s&SN=%s&key=%s'%(num,t,SN,key))
    r = requests.post('https://flocwise.cn/imgRecentLink?num=%s&time=%s&SN=%s&key=%s'%(num,t,SN,key), verify=True)
    # print('start ', t)
    if r.status_code == 200:
        data = json.loads(r.text)
        firstImgTime = 0
        lastImgTime  = time.time()
        if data['Success'] :
            showList = []          
            nameList = []  
            for camera in data['cameraList']:                
                showList.append([])
                nameList.append(camera['name'])                
                for image in camera['imgList']: 
                    showList[-1].append(image)         
                    # print(image['time'])
                    # print(image['link'])
                    if image['time'] > firstImgTime:
                        firstImgTime = image['time'] 
                    if image['time'] < lastImgTime:
                        lastImgTime = image['time'] 
            print('first last', firstImgTime, lastImgTime)
            print('自',time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(lastImgTime )),
                        '至',time.strftime('%H:%M:%S',time.localtime(firstImgTime )))
            return showList, nameList,firstImgTime, lastImgTime 
        else:
            print('获取%s图片失败 %s' % (SN, data['Error']))
            return [],[],0,0
    else:
        print('通讯失败',r.status_code)
        return [],[],0,0

def getRecentImageLink2(SN,num, t, last):    
    # addr = 'https://flocwise.cn/imgRecentLink2?num=%s&time=%s&SN=%s&last=%s'%(num,t,SN,last)
    # print(addr)
    r = requests.post('https://flocwise.cn/imgRecentLink2?num=%s&time=%s&SN=%s&last=%s'%(num,t,SN,last), verify=True)
    # print('start ', t)
    if r.status_code == 200:
        data = json.loads(r.text)
        firstImgTime = 0
        lastImgTime  = time.time()
        if data['Success'] :
            showList = []          
            nameList = []  
            for camera in  data['cameraList']:                
                showList.append([])
                nameList.append(camera['name'])
                for image in camera['imgList']: 
                    showList[-1].append(image)         
                    # print(image['link'])
                    if image['time'] > firstImgTime:
                        firstImgTime = image['time'] 
                    if image['time'] < lastImgTime:
                        lastImgTime = image['time'] 
            return showList, nameList,firstImgTime, lastImgTime 
        else:
            print('获取%s图片失败 %s' % (SN, data['Error']))
            return [],[],0,0
    else:
        print('通讯失败')
        return [],[],0
    



def getFlocStatics(SN, key, start, end, current ):
    ulr_addr = 'https://flocwise.cn/getFlocStatics?SN=%s&end=%s&start=%s&key=%s&time=%s' %(SN, end, start, key, current)
    # print(SN, key, start,end, current)
    r = requests.post(ulr_addr, verify=True)    
    if r.status_code == 200:
        data = json.loads(r.text)
        if data['Success'] == True:
            flocList = data['list']
            for floc in flocList:
                floc['time'] += int(current)
                # print(floc)
            return True, flocList
        else:
            print('无数据')
            return False, []    
    else:
        print('通讯失败')
        return False, []
    